//
//  HomeViewController.swift
//  MySwiftProject
//
//  Created by 蓓蕾 on 2021/11/11.
//

import UIKit

class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    private lazy var netData = NSDictionary()
    private lazy var banner = NSMutableArray()
    var cyclePictureView : JCyclePictureView!
    var tableView : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        getNetData()
        // Do any additional setup after loading the view.
    }
    
    func initUI()
    {
        let headerView = UIView(frame:CGRect(x: 0, y: 0, width: kScreenWidth, height: 800))
        
        let pictures:[String] = [ImagePrefix+"1c3894b8-63ae-11eb-bf87-00163e08b39e.png", ImagePrefix+"aebe8350-7e58-11eb-8c9c-00163e08b39e.png", ImagePrefix+"0af9bd72-e6a8-11ea-ab35-00163e08b39e.png"];
        self.cyclePictureView = JCyclePictureView(frame: CGRect(x: 0, y: 20, width: kScreenWidth, height: 200), pictures: pictures)
        headerView.addSubview(cyclePictureView)
        
//        let topItemView = HomeTopItemView()
//        headerView.addSubview(topItemView)
//        topItemView.topImageView.image = UIImage.init(systemName: "点滴")
//        topItemView.titleView.text = "点滴"
//        topItemView.snp.makeConstraints { make in
//            make.left.equalTo(self.view.snp_left).offset(20)
//            make.top.equalTo(cyclePictureView.snp_bottom).offset(10)
//            make.width.equalTo(80)
//            make.height.equalTo(80)
//        }
        // 点击回调
        cyclePictureView.didTapAtIndexHandle = { index in
            
            print("点击了第 \(index + 1) 张图片")
        }
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: .plain)
        self.view.addSubview(self.tableView)
        self.tableView.tableHeaderView = headerView
        self.tableView.delegate = self as UITableViewDelegate
        self.tableView.dataSource = self as UITableViewDataSource
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "UITableViewCell")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")!;
        cell.textLabel?.text = String(format: "\(indexPath.row)")
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 100;
    }
    
    func getNetData()
    {
        let parama = ["uid":"5449","customer_type":"兑换码用户"]
        RequestManager.shareInstance.postRequest(UrlString: Circle3, parama: parama) { result in
            self.netData = result.result.value as! NSDictionary
            CWLog("result = \(String(describing: self.netData))")
            let value = self.netData.object(forKey: "values") as! NSDictionary
            let banner = value.object(forKey: "banner") as! NSArray
            banner.forEach({ (bannerDic) in
                let bannerItem = bannerDic as! NSDictionary
                let imageName = bannerItem.object(forKey: "image")  as! NSString
                let imageUrl = String(format: "%@%@", ImagePrefix,imageName)// ImagePrefix.append(imageName as! String)
                self.banner.add(imageUrl)
            })
            self.cyclePictureView.pictures = self.banner as! [String];
        } failure: { failure in
            
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
